use std::fmt::Display;

use rbatis::rbdc::DateTime;

//unwrap
pub fn uw<T: Default + Clone>(s: &Option<T>) -> ::askama::Result<T> {
    let var_name = s.to_owned().unwrap_or_default();
    Ok(var_name)
}
//is_some
pub fn is<T: Default + Clone>(s: &Option<T>) -> ::askama::Result<bool> {
    Ok(s.is_some())
}
pub fn not_null<T: Clone + Display>(s: &Option<T>) -> ::askama::Result<bool> {
    let b = s.as_ref().map_or(false, |f| !f.to_string().is_empty());
    Ok(b)
}
pub fn is_null<T: Clone + Display>(s: &Option<T>) -> ::askama::Result<bool> {
    let b = s.as_ref().map_or(true, |f| f.to_string().is_empty());
    Ok(b)
}
//is_empty
pub fn is_empty<T>(s: &Vec<T>) -> ::askama::Result<bool> {
    Ok(s.is_empty())
}

pub fn not_empty<T>(s: &Vec<T>) -> ::askama::Result<bool> {
    Ok(!s.is_empty())
}
//
pub fn add<T: Clone + Display>(s: &Option<T>, a: &str) -> ::askama::Result<String> {
    let res = if s.as_ref().map_or(false, |f| !f.to_string().is_empty()) {
        format!("{}{}", a, s.clone().unwrap())
    } else {
        String::default()
    };
    Ok(res)
}
pub fn dt(s: &DateTime) -> ::askama::Result<String> {
    let format = s.format("YYYY-MM-DD hh:mm:ss");
    Ok(format)
}
pub fn fmtdt(s: &DateTime) -> ::askama::Result<String> {
    let format = s.format("YYYY-MM-DD");
    Ok(format)
}

pub fn sp(s: &String, d: char) -> ::askama::Result<Vec<(u32, String)>> {
    let vec: Vec<(u32, String)> = s
        .split(d)
        .enumerate()
        .map(|(i, s)| (i.try_into().unwrap(), s.to_string()))
        .collect();
    Ok(vec)
}
